package com.xinchuang.service.targetSystem.Impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinchuang.common.Result;
import com.xinchuang.common.ResultUtil;
import com.xinchuang.entity.targetSystem.AirPlane;
import com.xinchuang.mapper.targetSystem.AirPlaneMapper;
import com.xinchuang.service.targetSystem.AirPlaneService;
import com.xinchuang.vo.AirPlaneVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author zkwczx
 * @version 1.0
 * @date 2024/3/12 9:05
 */
@Service
public class AirPlaneServiceImpl extends ServiceImpl<AirPlaneMapper, AirPlane> implements AirPlaneService {

    private String DEFAULT_PLANE_ID = "0";

    @Override
    public Result addOrUpdateAirPlane(AirPlaneVo airPlane) {
        AirPlane air = new AirPlane();
        if (StringUtils.isNotBlank(airPlane.getId()) && DEFAULT_PLANE_ID.equals(airPlane.getId())) {
            air.setId(null);
        } else {
            air.setId(airPlane.getId());
        }
        air.setName(airPlane.getName());
        try {
            air.setLength(Double.parseDouble(airPlane.getLength()));
            air.setWidth(Double.parseDouble(airPlane.getWidth()));
            air.setDistance(Double.parseDouble(airPlane.getDistance()));
        } catch (Exception e) {
            return ResultUtil.error(500, "请检查长度/宽度/最大飞行距离，是否为数值！");
        }
        saveOrUpdate(air);
        return ResultUtil.success();
    }

    @Override
    public Result deletePlane(String id) {
        removeById(id);
        return ResultUtil.success();
    }

    @Override
    public Result selectPlaneOnly(String id) {
        return ResultUtil.success("查询成功", getById(id));
    }

    @Override
    public Result selectPlaneList(Boolean isPull, Integer pageNum, Integer pageSize) {
        if (null != isPull && isPull) {
            List<AirPlane> airPlanes = list();
            if (CollectionUtil.isEmpty(airPlanes)) {
                return ResultUtil.success(ListUtil.empty());
            }
            List<Map<String, Object>> list = new ArrayList<>();
            Map<String, Object> map;
            for (AirPlane plane : airPlanes) {
                map = new HashMap<>();
                map.put("label", plane.getName());
                map.put("value", plane.getId());
                list.add(map);
            }
            return ResultUtil.success(list);
        }
        Page<AirPlane> planePage = page(new Page<>(pageNum, pageSize));
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("pagecur", planePage.getCurrent());
        resultMap.put("pagesize", planePage.getSize());
        resultMap.put("total", planePage.getTotal());
        resultMap.put("pages", planePage.getPages());
        if (CollectionUtil.isEmpty(planePage.getRecords())) {
            resultMap.put("dataList", ListUtil.empty());
        } else {
            resultMap.put("dataList", planePage.getRecords());
        }
        return ResultUtil.success(resultMap);
    }

}
